package callnumber.gtdev5.com.photoVault.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.gtdev5.geetolsdk.mylibrary.util.SpUtils;
import com.gtdev5.geetolsdk.mylibrary.util.ToastUtils;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import callnumber.gtdev5.com.photoVault.R;
import callnumber.gtdev5.com.photoVault.adapter.VidoeAdapter;
import callnumber.gtdev5.com.photoVault.base.BaseFragment;
import callnumber.gtdev5.com.photoVault.bean.VideoBean;
import callnumber.gtdev5.com.photoVault.constants.AppConstans;
import callnumber.gtdev5.com.photoVault.utils.IsChoose;

public class VideoFragment extends BaseFragment {

    Unbinder unbinder;
    @BindView(R.id.iv_add)
    ImageView iv_add;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.dele)
    ImageView dele;
    @BindView(R.id.head_relative)
    RelativeLayout head_relative;
    private VidoeAdapter adapter;
    private List<String> list = new ArrayList<String>();
    private List<VideoBean> pName;


    ArrayList<String> mDatas;
    private static final int COMPLETED = 0;

    @Override
    protected int getLayouId() {
        return R.layout.fragment_video;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        Log.e("TAG", "SettingFragment+initViews");
    }

    @Override
    protected void initDatas() {
        boolean isFirstLoadTimeData = SpUtils.getInstance().getBoolean(AppConstans.ISFIRSTLOADTIMEDATA, true);
        if (isFirstLoadTimeData) {
            SpUtils.getInstance().putBoolean(AppConstans.ISFIRSTLOADTIMEDATA, false);
        }


    }


    @Override
    protected void initAction() {
        GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
        mRecyclerView.setLayoutManager(manager);
//      获取数据，向适配器传数据，绑定适配器.
        pName = new ArrayList<>();
        pName = DataSupport.findAll(VideoBean.class);
        if (pName != null && !pName.isEmpty()) {
            for (VideoBean phNameBean : pName) {
                list.add(phNameBean.getName());
            }
            mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        } else {
            list = initData();

        }

        adapter = new VidoeAdapter(getActivity(), list);
        mRecyclerView.setAdapter(adapter);
        iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert_edit();
            }
        });
        dele.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iSSHOW = iSSHOW == GONECHECK ? SHOWCHECK : GONECHECK;
                adapter.setMode(iSSHOW);
            }
        });
    }


    protected ArrayList<String> initData() {
        mDatas = new ArrayList<String>();
        for (int i = 0; i < 4; i++) {
            mDatas.add("默认视频");
            VideoBean phNameBean = new VideoBean();
            phNameBean.setName("默认视频");
            phNameBean.setIsSelect(0);
            phNameBean.setNum((i));
            phNameBean.setId(0);
            phNameBean.save();
        }
        return mDatas;
    }

    String newName;

    public void alert_edit() {
        final EditText et = new EditText(getContext());
        new AlertDialog.Builder(getContext()).setTitle("请输入文件夹名称")
//                .setIcon(android.R.drawable.sym_def_app_icon)
                .setView(et)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //按下确定键后的事件
                        for (int n = 0; n < list.size(); n++) {
                            if (et.getText().toString().equals(list.get(n))) {
                                ToastUtils.showShortToast("命名重复！");
                                return;
                            } else if (TextUtils.isEmpty(et.getText().toString())) {
                                ToastUtils.showShortToast("命名不能为空！");
                                return;
                            }
                        }
                        newName = et.getText().toString();
                        adapter.addData(list.size(), newName);
                    }
                }).setNegativeButton("取消", null).show();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    private int iSSHOW = GONECHECK;
    private static final int GONECHECK = 0;
    private static final int SHOWCHECK = 1;

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


}
